package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.WmsMaterialInfoMapper;
import com.ruoyi.wms.domain.WmsMaterialInfo;
import com.ruoyi.wms.service.IWmsMaterialInfoService;
import com.ruoyi.common.core.text.Convert;

/**
 * 物料信息管理Service业务层处理
 * 
 * @author 杨懿
 * @date 2023-11-27
 */
@Service
public class WmsMaterialInfoServiceImpl implements IWmsMaterialInfoService 
{
    @Autowired
    private WmsMaterialInfoMapper wmsMaterialInfoMapper;
    @Autowired
    private ISysConfigService configService;
    /**
     * 查询物料信息管理
     * 
     * @param id 物料信息管理主键
     * @return 物料信息管理
     */
    @Override
    public WmsMaterialInfo selectWmsMaterialInfoById(Long id)
    {
        return wmsMaterialInfoMapper.selectWmsMaterialInfoById(id);
    }

    /**
     * 查询物料信息管理列表
     * 
     * @param wmsMaterialInfo 物料信息管理
     * @return 物料信息管理
     */
    @Override
    public List<WmsMaterialInfo> selectWmsMaterialInfoList(WmsMaterialInfo wmsMaterialInfo)
    {
        return wmsMaterialInfoMapper.selectWmsMaterialInfoList(wmsMaterialInfo);
    }

    /**
     * 新增物料信息管理
     * 
     * @param wmsMaterialInfo 物料信息管理
     * @return 结果
     */
    @Override
    public int insertWmsMaterialInfo(WmsMaterialInfo wmsMaterialInfo)
    {
        wmsMaterialInfo.setCreateTime(DateUtils.getNowDate());
        return wmsMaterialInfoMapper.insertWmsMaterialInfo(wmsMaterialInfo);
    }

    /**
     * 修改物料信息管理
     * 
     * @param wmsMaterialInfo 物料信息管理
     * @return 结果
     */
    @Override
    public int updateWmsMaterialInfo(WmsMaterialInfo wmsMaterialInfo)
    {
        wmsMaterialInfo.setUpdateTime(DateUtils.getNowDate());
        return wmsMaterialInfoMapper.updateWmsMaterialInfo(wmsMaterialInfo);
    }

    /**
     * 批量删除物料信息管理
     * 
     * @param ids 需要删除的物料信息管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsMaterialInfoByIds(String ids)
    {
        return wmsMaterialInfoMapper.deleteWmsMaterialInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除物料信息管理信息
     * 
     * @param id 物料信息管理主键
     * @return 结果
     */
    @Override
    public int deleteWmsMaterialInfoById(Long id)
    {
        return wmsMaterialInfoMapper.deleteWmsMaterialInfoById(id);
    }
    @Override
    public int updateWmsMaterialTotal(WmsMaterialInfo wmsMaterialInfo){
        return wmsMaterialInfoMapper.updateWmsMaterialTotal(wmsMaterialInfo);
    }

    @Override
    public String importMaterial(List<WmsMaterialInfo> materialList,Long deptId)
    {
        if (StringUtils.isNull(materialList) || materialList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (WmsMaterialInfo bean : materialList)
        {
            try
            {
                bean.setDeptId(deptId);
                if(bean.getTotal()== null ) {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、物品 " + bean.getName() + " 入库数量不能为空：";
                    failureMsg.append(msg);
                    continue;
                }
                String code = "";
                if(StringUtils.isEmpty(bean.getCode())){
                    int num =  Integer.parseInt(configService.selectConfigByKey("m.code")) ;
                    SysConfig config = new SysConfig();
                    config.setConfigKey("m.code");
                    config.setConfigValue((++num)+"");
                    configService.updateConfigValue(config);
                    code = StringUtils.getCode(code ,num);;
                }else {
                    code = bean.getCode();
                }
                // 验证是否存在这个用户
                WmsMaterialInfo tem = new WmsMaterialInfo();
                tem.setCode(code);
                List<WmsMaterialInfo> list = wmsMaterialInfoMapper.selectWmsMaterialInfoList(tem);
                if (list != null && list.size()>0)
                {//更新数量
                    list.get(0).setTotal(list.get(0).getTotal().add(bean.getTotal()));
                    wmsMaterialInfoMapper.updateWmsMaterialTotal(list.get(0));
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、物品 " +bean.getName()  + " 更新成功");
                }
                else{
                    //增加
                    wmsMaterialInfoMapper.insertWmsMaterialInfo(bean);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、物品 " + bean.getName()  + " 导入成功");

                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum  + "、物品 " + bean.getName()  + " 导入失败：";
                failureMsg.append(msg + e.getMessage());

            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
